package com.deyuanyun.pic.ae.service;

import com.alibaba.fastjson.JSONArray;
import com.deyuanyun.pic.ae.enumeration.DataStatedEnum;
import com.deyuanyun.pic.common.spring.SpringAppliction;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.webservice.UtilsServiceClient;
import com.deyuanyun.pic.pipe.domain.CenterLine;
import com.deyuanyun.pic.pipe.domain.Identification;
import com.deyuanyun.pic.pipe.service.PipeCenterLineService;
import com.deyuanyun.pic.pipe.util.PipeGeneralUtil;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by linwei on 11/10/2016.
 */
public abstract class AEBaseService {

    /**
     * 日志
     */
    protected Logger logger = LogManager.getLogger(this.getClass());

    protected String realName = "";

    /**
     * 得到管道的gis坐标id
     *
     * @param pipeLineId
     * @return
     */
    protected String getPipeLineGisId(String pipeLineId){
        PipeCenterLineService pipeCenterLineService = SpringAppliction.getBean(PipeCenterLineService.class);
        CenterLine centerLine = pipeCenterLineService.getCenterLineByLineId(pipeLineId);
        if (centerLine != null){
            return centerLine.getGisId();
        }
        return null;
    }

    /**
     * 判断是否已经审核
     *
     * @param dateStated
     * @return
     */
    protected boolean isAudit(String dateStated) {
        return DataStatedEnum.AUDIT_RELEASED.getCode().equals(dateStated)
                || DataStatedEnum.AUDIT_DISMISSED.getCode().equals(dateStated);
    }

    /**
     * 判断是否 审核通过
     *
     * @param dateStated
     * @return
     */
    protected boolean isAuditReleased(String dateStated) {
        return DataStatedEnum.AUDIT_RELEASED.getCode().equals(dateStated);
    }

    /**
     * 判断是否 审核驳回
     *
     * @param dateStated
     * @return
     */
    protected boolean isAuditDismissed(String dateStated) {
        return DataStatedEnum.AUDIT_DISMISSED.getCode().equals(dateStated);
    }

    /**
     * 根据经纬度得到位置信息
     *
     * @param pipeLineId
     * @param x
     * @param y
     * @return
     */
    protected PositionObj getPositionInfo(String pipeLineId, BigDecimal x, BigDecimal y) {

        String gisId = this.getPipeLineGisId(pipeLineId);
        PositionObj positionObj = new PositionObj();
        // 计算起始绝对里程
        BigDecimal startMileage = getMileage(gisId, x, y);
        Identification identification = PipeGeneralUtil.findLatelyIdentification(pipeLineId, startMileage);
        if (startMileage != null) {
            positionObj.setMileage(((new BigDecimal(1000).multiply(startMileage)).setScale(0, BigDecimal.ROUND_HALF_UP)).longValue());
        }
        if (identification != null) {
            positionObj.setNagm(identification.getId());
            positionObj.setNagmNumber(identification.getSignNumber());
            positionObj.setNagmMileage(PipeGeneralUtil.calculation(startMileage, identification.getMileage()).multiply(new BigDecimal(1000)).longValue());
        }
        return positionObj;
    }

    protected class PositionObj {
        // 绝对距离
        private Long mileage;
        // 最近参考点id
        private String nagm;
        // 最近参考点编号
        private String nagmNumber;
        // 相对最近参考点距离
        private Long nagmMileage;

        public Long getMileage() {
            return mileage;
        }

        public void setMileage(Long mileage) {
            this.mileage = mileage;
        }

        public String getNagm() {
            return nagm;
        }

        public void setNagm(String nagm) {
            this.nagm = nagm;
        }

        public String getNagmNumber() {
            return nagmNumber;
        }

        public void setNagmNumber(String nagmNumber) {
            this.nagmNumber = nagmNumber;
        }

        public Long getNagmMileage() {
            return nagmMileage;
        }

        public void setNagmMileage(Long nagmMileage) {
            this.nagmMileage = nagmMileage;
        }
    }

    /**
     * 获得绝对距离
     *
     * @param id
     * @param x
     * @param y
     * @return
     */
    private BigDecimal getMileage(String id, BigDecimal x, BigDecimal y) {

        BigDecimal mileage = null;

        if (StringUtils.isNullEmpty(id) || x == null || y == null) {
            return mileage;
        }

        List<Map> list = new ArrayList<Map>();
        Map map = new HashMap();
        map.put("id", id);
        map.put("x", x);
        map.put("y", y);
        list.add(map);
        String parrm = JSONArray.toJSONString(list);
        try {
            List<Double> mileageList = UtilsServiceClient.getMileage(parrm);
            if (mileageList != null && mileageList.size() > 0){
                return BigDecimal.valueOf(mileageList.get(0));
            }
        } catch (MalformedURLException e) {
            logger.error("--------->调用webService计算绝对距离失败");
        }
        return mileage;
    }
}
